package com.recommend.service.admin.impl;

import com.recommend.domain.EvaluateDomain;
import com.recommend.domain.MovieDomain;
import com.recommend.domain.RecommendDomain;
import com.recommend.domain.ScoreDomain;
import com.recommend.persistence.EvaluateDomainMapper;
import com.recommend.persistence.MovieDomainMapper;
import com.recommend.persistence.RecommendDomainMapper;
import com.recommend.persistence.ScoreDomainMapper;
import com.recommend.service.EvaluateService;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by lw.
 */
@Service
@Transactional
public class EvaluateServiceImpl implements EvaluateService{
    private final static Logger logger = org.slf4j.LoggerFactory.getLogger(EvaluateServiceImpl.class);

    @Resource
    private EvaluateDomainMapper evaluateDomainMapper;
    @Resource
    private ScoreDomainMapper scoreDomainMapper;
    @Resource
    private MovieDomainMapper movieDomainMapper;
    @Resource
    private RecommendDomainMapper recommendDomainMapper;

    @Override
     public void insertEvaluate(EvaluateDomain evaluateDomain){
        evaluateDomainMapper.insertSelective(evaluateDomain);

        //更新score表
        float avgScore = evaluateDomainMapper.selectAvgScore(evaluateDomain.getMovieId());
        ScoreDomain scoreDomain = new ScoreDomain();
        scoreDomain.setMovieId(evaluateDomain.getMovieId());
        scoreDomain.setGrade(avgScore);
        scoreDomainMapper.updateByPrimaryKeySelective(scoreDomain);

        //更新recommend表
        int customerId = evaluateDomain.getCustomerId();
        MovieDomain movieDomain = new MovieDomain();
        MovieDomain movieDomain1 = movieDomainMapper.selectByPrimaryKey(evaluateDomain.getMovieId());
        movieDomain.setAction(movieDomain1.getAction());
        movieDomain.setAdventure(movieDomain1.getAdventure());
        movieDomain.setAnimation(movieDomain1.getAnimation());
        movieDomain.setChildren(movieDomain1.getAnimation());
        movieDomain.setComedy(movieDomain1.getComedy());
        movieDomain.setCrime(movieDomain1.getCrime());
        movieDomain.setFantasy(movieDomain1.getFantasy());
        movieDomain.setRomance(movieDomain1.getFantasy());
        List<Integer> movieList = movieDomainMapper.selectRecommendMovieByMovieId(movieDomain);
        for(Integer movieId : movieList){
            RecommendDomain recommendDomain = new RecommendDomain();
            recommendDomain.setCustomerId(customerId);
            recommendDomain.setMovieId(movieId);
            recommendDomainMapper.insertSelective(recommendDomain);
        }
    }

    @Override
    public EvaluateDomain getByCustomerId(int customerId){
        return evaluateDomainMapper.selectByCustomerId(customerId);
    }
}
